package com.pureland.security.entity;

import lombok.Builder;
import lombok.Data;
import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @projectName: pureland_cloud
 * @className: com.pureland.security.entity.AuthInfo
 * @description: 认证用户信息封装类
 * @author: tong.li
 * @createTime: 2020/11/17 16:47
 * @version: v1.0
 * @copyright: 版权所有 李彤 © 2020
 */
@Data
@Builder
public class AuthInfo implements UserDetails, CredentialsContainer, Serializable {

    private static final long serialVersionUID = 111188854543223L;

    /**
     * 用户ID
     */
    private Long userId;
    /**
     * 用户Email
     */
    private String email;
    /**
     * 用户性别(true-男,false女)
     */
    private Boolean sex;
    /**
     *
     */
    private String mobile;
    /**
     * 公司ID
     */
    private Long tenantId;
    /**
     * 公司社会信用代码
     */
    private String tenantCode;
    /**
     * 公司名称
     */
    private String companyName;
    /**
     * 昵称
     */
    private String nickName;
    /**
     * 真实姓名
     */
    private String realName;
    /**
     * 登录名称
     */
    private String username;
    /**
     * 登录密码
     */
    private String password;
    /**
     * 头像
     */
    private String avatar;

    /**
     * 用户状态(-2禁用,-1未激活,0-正常,1-永久锁定)
     */
    private Byte status;

    /**
     * 部门列表
     */
    private List<SecurityDepartment<Long>> departments;
    /**
     * 权限列表
     */
    private Collection<String> permissions = new ArrayList<>();
    /**
     * 角色列表
     */
    private Collection<String> roles = new ArrayList<>();
    /**
     * 授权相关信息
     */
    private Collection<GrantedAuthority> authorities;

    /**
     * 账户是否正常标识，常用于内部代码判断
     */
    private boolean enabled;


    @Override
    public void eraseCredentials() {
        this.password = null;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return false;
    }

    public void setStatus(Byte status) {
        this.status = status;
        // 设置是否启用状态
        if (status != null && status == 0) {
            this.enabled = true;
        }
    }
}
